
import conf from '../config/userconf.js'
export default{
    data(){
        return{
            list: [],  //分类列表数据
            current: 1,  //分类当前页码
            dialogTableVisible: false,   //嵌套表格隐藏展示
            st_id: 1,  // 当前的分类id
            st_name: '',  // 当前的分类名称
            searchData: {
                searchName: '',  //查询分类名称
            }, 
            levelId : '',   //上级分类id   1 房源  2 客源
            addtypeform: {    //添加分类用途
                st_name: '',
                st_superior_id: '',
                st_type_name: ''
            },
            addrules:{
                st_name: [
                    { required: true, message: '请输入分类名称', trigger: 'blur'}
                ],
            },
            addtypeVisible: false,
            addtypeTit: '添加分类',  
            edittypeId: '',  //修改分类时的分类id
        }
    },
    filters:{

    },
    methods:{
        /* 分类列表*/
        getList (data=Object()){

            console.log('------------id-------------',this.$route.query.id);

            if(data.searchName){
                data.st_name = data.searchName
            }
            data.page = this.current;
            data.st_id = this.$route.query.id;
            conf.leveltype.list(data,(state,res)=>{
                if (state == 'success'&&res.data.code == 0){
                    console.log(res);
                    this.list = res.data.data;
                }else if(state == 'success'&&res.data.code != 0){
                    // console.log(res);
                    this.$message({
                        type: 'info',
                        message: res.data.msg
                    });
                }
            })
        },
        add(){
            this.addtypeTit = '添加分类';
            this.addtypeVisible = true;
        },
        edit(row){
            console.log(row);
            this.addtypeTit = '修改分类';
            this.addtypeform = {
                st_name: row.st_name,
                st_superior_id: row.st_superior_id,
                st_type_name: row.st_type_name
            }
            this.edittypeId = row.st_id;
            this.addtypeVisible = true;
        },
        // 添加分类  修改
        typeFun(formName,data=Object()){
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if(this.addtypeTit === '添加分类'){
                        conf.leveltype.add(this.addtypeform,(state,res)=>{
                            if(state == 'success'&&res.data.code == 0){
                                // console.log(res);
                                this.getList();
                                this.$message({
                                    type: 'success',
                                    message: '添加成功'
                                });
                                this.addtypeVisible = false;
                            }else{
                                this.$message({
                                    type: 'info',
                                    message: res.data.msg
                                });
                            }
                        })
                    }else if(this.addtypeTit === '修改分类'){
                        
                        data = this.addtypeform;
                        data.st_id = this.edittypeId;
                        console.log(data);
                        conf.leveltype.edit(data,(state,res)=>{
                            if(state == 'success'&&res.data.code == 0){
                                // console.log(res);
                                this.getList();
                                this.$message({
                                    type: 'success',
                                    message: '修改成功'
                                });
                                this.addtypeVisible = false;
                            }else{
                                // console.log(res);
                                this.$message({
                                    type: 'info',
                                    message: res.data.msg
                                });
                            }
                        })
                    }
                    
                      
                } else {
                  // console.log('error submit!!');
                  return false;
                }
            });

        },
        //取消添加分类
        canceladd(formName){
            this.addtypeVisible = false;
            if (formName) {
                this.$refs[formName].resetFields();
            }
        },
        // 查看分类下种类
        look(row){
            // this.st_id = row.st_id;
            // this.st_name = row.st_name;
            this.$router.push({
                path: '/last_leveltypelist',
                query: {
                    first_id: this.$route.query.id,
                    st_id: row.st_id,
                    st_name: row.st_name
                }
            })
        },
        // 删除分类
        del(row,data=Object()){
           console.log(row);
            this.$confirm('确认删除分类 "'+row.st_name+'" ?', '删除分类', {  
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {

                data.st_id = row.st_id;
                conf.leveltype.del(data,(state,res)=>{
                    if(state == 'success'&&res.data.code == 0){
                        this.getList();
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                          });
                    }else if(state == 'success'&&res.data.code != 0){
                        this.$message({
                            type: 'info',
                            message: res.data.msg
                        });
                    }
                })

            }).catch(() => {
                this.$message({
                  type: 'info',
                  message: '已取消删除'
                });          
            });
        },
        // 分类 切换页码
        handleCurrentChange(val) {
            this.current = val;
            this.getList();
        },
        //查询分类
        search(){
            let data  = this.searchData;
            this.getList(data);
        },
    },
    mounted (){
        this.$nextTick(function(){
            this.getList();
        })
    },
    // 计算属性
    computed :{
        
    }
}